import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home/Home'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  {
    path: '/home', component: Home, children: [
      { path: 'login', component: () => import('@/views/User/Login') },
      { path: 'reguser', component: () => import('@/views/User/reguser') },
      { path: 'todaymusic', component: () => import('@/views/ToDayMusic/ToDayMusic') }
    ]
  },
  { path: '/search/:keyword?', component: () => import('@/views/Search/Search') },
  { path: '/albums/:id', component: () => import('@/views/Albums/Albums') },
  { path: '/artists/:id', component: () => import('@/views/Artists/Artists') },
  { path: '/playlists/:id', component: () => import('@/views/PlayLists/PlayLists') },
  { path: '/userprofiles/:id', component: () => import('@/views/UserProfiles/UserProfiles') },
  { path: '/mv/:id', component: () => import('@/views/Mv/Mv'), meta: { noShow: true } }

]

const router = new VueRouter({
  routes,
  scrollBehavior() {
    return { x: 0, y: 0 }
  }
})

// 全局路由前置守卫
router.beforeEach((to, form, next) => {
  if (localStorage.getItem('token') && to.path == '/home/login') {
    next(false)
  } else {
    next()
  }

})

// 重写push方法，解决跳转相同路由报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}


export default router
